home *** CD-ROM | disk | FTP | other *** search
- From: clamage@Eng.Sun.COM (Steve Clamage)
- Message-ID: <4ikmol$b5u@engnews1.Eng.Sun.COM>
- X-Original-Date: 18 Mar 1996 22:06:13 GMT
- Path: in2.uu.net!bounce-back
- Date: 19 Mar 96 01:16:50 GMT
- Approved: fjh@cs.mu.oz.au
- Newsgroups: comp.std.c++
- Subject: Re: FOR comp.std.c++: problems with I/O except
- Organization: Sun Microsystems Inc.
- References: <199603181627.RAA08990@bredex.bredex.de>
- Reply-To: clamage@Eng.Sun.COM
- X-Auth: PGPMoose V1.1 PGP comp.std.c++
- iQBFAgUBMU49BOEDnX0m9pzZAQE+dgF/QndLdHSVOgBUQjNOvpuAIvg9s5G5CNF7
- cumOhtISUsm+F9Qv9dcB2iZhHTcxvwMZ
- =7DDD
-
- In article RAA08990@bredex.bredex.de, Nico Josuttis <nico@bredex.de> writes:
- >Hi,
- >Trying the new feature of throwing stream exceptions
- >i wrote the following program:
-
- [ trimmmed ]
-
- > try {
- > int value, sum;
- >
- > /* while not EOF
- > * read and add value
- > */
- > sum = 0;
- > while (cin >> value) {
- > sum += value;
- > }
- >
- > cout << "sum: " << sum << endl;
- >
- > }
- > catch (ios::failure error) {
- > cerr << "Error: " << error.what() << endl;
- > exit (EXIT_FAILURE);
- > }
-
- >But it didn't work as I expected, because also on EOF
- >I ALWAYS got the failbit exception!
-
- >The problem is, that istream::ipfx() always calls setstate(failbit),
- >if after any preparation good() is false.
- >So even if only the eofbit is set (due to the skip of whitespace),
- >the failbit is also set.
-
- That is the desired behavior, and your program should take it into account.
-
- For example, suppose the file ends with whitespace (which it often will,
- if only because of a final newline before the EOF). You attempt to read
- an int value, whitespace is skipped, and EOF is reached before any
- valid characters are found. That is a failure, by definition. The failure
- cannot be predicted before starting the input, since characters are
- available at the start. As it happens, you are not interested in any
- of the characters which are available.
-
- Now suppose that EOF is already true when starting the input. Again by
- definition, input fails, since no characters are available for reading.
-
- The ipfx() function is called as part of starting an input operation.
- You are asking for input, and to be notified if the input fails. It fails
- -- no input available -- and you are notified.
-
- When you have a loop like
- while( cin >> value ) { ... }
- it will ALWAYS end by failing, since the loop only ends by finding improper
- input or reading past EOF, and reading past EOF is a failure. Thus,
- you probably do not want to use an exception to detect the end of the
- loop.
-
- > ... In general I need a chance to handle eof without getting
- >failbit exceptions.
-
- I think it is more likely that you don't want to throw an exception on
- failure unless failure is not expected to happen. When reading input up
- to the end, failure will always occur. A schema like the following
- gives you the control you want without using exceptions:
-
- while( cin >> value ) {
- ... do something with value
- }
- if( cin.eof() ) ... no more input available
- else ... error in input
-
- More likely, you would write a single test like this:
- if( ! cin.eof() ) ... error in input
-
- Exceptions are best used for those cases where you don't expect
- them to be triggered, and you need to escape to a higher level in the
- program when they do occur. Exceptions give you a way to escape without
- having to pass error status explicitly through a chain of function calls.
- Don't use exceptions for normal loop control; they are inefficient for
- that purpose and cause you to write more complicated code instead of
- less complicated code.
-
- ---
- Steve Clamage, stephen.clamage@eng.sun.com
- ---
- [ comp.std.c++ is moderated. To submit articles: try just posting with ]
- [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
- [ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
- [ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
- [ Comments? mailto:std-c++-request@ncar.ucar.edu ]
-